Add --open flag to cargo doc
authorPierre Krieger <pierre.krieger1708@gmail.com>
Wed, 1 Oct 2014 06:53:05 +0000 (08:53 +0200)
committerPierre Krieger <pierre.krieger1708@gmail.com>
Thu, 2 Oct 2014 17:40:42 +0000 (19:40 +0200)
src/bin/doc.rs
src/cargo/ops/cargo_doc.rs

index d0635359844e35a0800ccf24ae8028fa662d5c6d..601a284dbfa0013d15d7b387731c017496e41112 100644 (file)
@@ -13,6 +13,7 @@ Usage:
 
 Options:
     -h, --help              Print this message
+    --open                  Opens the docs in a browser after the operation
     --no-deps               Don't build documentation for dependencies
     -j N, --jobs N          The number of jobs to run in parallel
     --features FEATURES     Space-separated list of features to also build
@@ -33,6 +34,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
 
     let mut doc_opts = ops::DocOptions {
         all: !options.flag_no_deps,
+        open_result: options.flag_open,
         compile_opts: ops::CompileOptions {
             env: if options.flag_no_deps {"doc"} else {"doc-all"},
             shell: shell,
index 486b5e46f55656270628c0e04a8a8d85a8ae4fb4..2a34f7fd4706b26f750b757de502b6c027128ed8 100644 (file)
@@ -3,10 +3,12 @@ use std::collections::HashSet;
 use core::source::Source;
 use ops;
 use sources::PathSource;
+use std::io::process::Command;
 use util::{CargoResult, human};
 
 pub struct DocOptions<'a> {
     pub all: bool,
+    pub open_result: bool,
     pub compile_opts: ops::CompileOptions<'a>,
 }
 
@@ -34,5 +36,54 @@ pub fn doc(manifest_path: &Path,
     }
 
     try!(ops::compile(manifest_path, &mut options.compile_opts));
+
+    if options.open_result {
+        use std::io::fs::PathExtensions;
+
+        match lib_names.iter().nth(0).map(|l| package.get_absolute_target_dir()
+                                                     .join("doc").join(*l).join("index.html"))
+        {
+            Some(ref path) if path.exists() => open_docs(path),
+            _ => ()
+        }
+    }
+
     Ok(())
 }
+
+#[cfg(not(any(target_os = "windows", target_os = "macos")))]
+fn open_docs(path: &Path) {
+    // trying xdg-open
+    match Command::new("xdg-open").arg(path).detached().status() {
+        Ok(_) => return,
+        Err(_) => ()
+    };
+
+    // trying gnome-open
+    match Command::new("gnome-open").arg(path).detached().status() {
+        Ok(_) => return,
+        Err(_) => ()
+    };
+
+    // trying kde-open
+    match Command::new("kde-open").arg(path).detached().status() {
+        Ok(_) => return,
+        Err(_) => ()
+    };
+}
+
+#[cfg(target_os = "windows")]
+fn open_docs(path: &Path) {
+    match Command::new("start").arg(path).detached().status() {
+        Ok(_) => return,
+        Err(_) => ()
+    };
+}
+
+#[cfg(target_os = "macos")]
+fn open_docs(path: &Path) {
+    match Command::new("open").arg(path).detached().status() {
+        Ok(_) => return,
+        Err(_) => ()
+    };
+}